/**
 * 获取文章、顶部大图
 * */

$(document).ready(
    function () {

        // 获取主机地址之后的目录
        var curWwwPath = window.document.location.href;


        /**
         * 获取项目根路径
         *
         * @returns {string} 返回路径地址
         */
        function getRootPath() {
            //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
            var curWwwPath = window.document.location.href;
            //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
            var pathName = window.document.location.pathname;
            var pos = curWwwPath.indexOf(pathName);
            //获取主机地址，如： http://localhost:8083
            var localhostPaht = curWwwPath.substring(0, pos);
            //获取带"/"的项目名，如：/uimcardprj
            var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
            return (localhostPaht + projectName);
        }

        /**
         * 截取url
         * @returns {Object}
         * @constructor
         */
        function GetRequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }

        var Request = new Object();
        Request = GetRequest();


        //当前页
        var pageNo;

        //总页数
        var pages;

        //获取最新活动
        function getActivity(oneClass, twoClass, pageNo, pageSize) {
            //设置默认值
            if (pageNo == undefined || pageNo == '') {
                pageNo = 1;
            }
            if (pageSize == undefined || pageSize == '') {
                pageSize = 5;
            }
            $.ajax({
                type: "POST",
                async: false,
                url: getRootPath() + "/CI.do",
                data: "oneClass=" + oneClass + "&twoClass=" + twoClass + "&pageNo=" + pageNo + "&pageSize=" + pageSize,
                success: function (data) {
                    //当前页
                    pageNo = data.data.articleContext.pageNo;

                    //总页数
                    pages = data.data.articleContext.pages;

                    //大图
                    $("#activityImg").attr("src", 'resource/image/' + data.data.topImagePath);

                    //默认隐藏
                    $("#oneActivity").hide();
                    $("#twoActivity").hide();
                    $("#threeActivity").hide();
                    $("#fourActivity").hide();
                    $("#fiveActivity").hide();

                    //清空处理
                    $("#pageUl").empty()
                    //上一页按钮样式
                    $("#pageUl").append('<li><a id="reviousId" aria-label="Previous"><span aria-hidden="true"><img src="img/leftb.png" alt=""></span></a></li>');

                    if (pageNo + 4 < pages) {
                        for (var thisP = pageNo; thisP <= pageNo + 4; thisP++) {
                            if (thisP == pageNo) {
                                //设定当前背景颜色
                                $("#pageUl").append('<li><a class="flipcol" >' + thisP + '</a></li>');
                            } else {
                                $("#pageUl").append('<li><a id="clickPage">' + thisP + '</a></li>');
                            }
                        }
                    } else {
                        for (var thisP = pageNo; thisP <= pages; thisP++) {
                            if (thisP == pageNo) {
                                //设定当前背景颜色
                                $("#pageUl").append('<li><a class="flipcol" >' + thisP + '</a></li>');
                            } else {
                                $("#pageUl").append('<li><a id="clickPage">' + thisP + '</a></li>');
                            }
                        }
                    }

                    //下一页按钮样式
                    $("#pageUl").append('<li><a id="nextId" aria-label="Next"><span aria-hidden="true"><img class="imgr" src="img/rightb.png" alt=""></span></a></li>');

                    $.each(data.data.articleContext.dataList, function (key, article) {
                        if (key == 0) {
                            $("#oneActivity a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                            $("#oneActivity a img").attr("src", 'resource/image/' + article.resource[0].pathName);
                            $("#oneActivity a div p").html(article.titleName);
                            $("#oneActivity a div p:eq(1)").append(article.content);
                            $("#oneActivity").show();
                        } else if ((key == 1)) {
                            $("#twoActivity a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                            $("#twoActivity a img").attr("src", 'resource/image/' + article.resource[0].pathName);
                            $("#twoActivity a div p").html(article.titleName);
                            $("#twoActivity a div p:eq(1)").append(article.content);
                            $("#twoActivity").show();
                        } else if ((key == 2)) {
                            $("#threeActivity a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                            $("#threeActivity a img").attr("src", 'resource/image/' + article.resource[0].pathName);
                            $("#threeActivity a div p").html(article.titleName);
                            $("#threeActivity a div p:eq(1)").append(article.content);
                            $("#threeActivity").show();
                        } else if ((key == 3)) {
                            $("#fourActivity a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                            $("#fourActivity a img").attr("src", 'resource/image/' + article.resource[0].pathName);
                            $("#fourActivity a div p").html(article.titleName);
                            $("#fourActivity a div p:eq(1)").append(article.content);
                            $("#fourActivity").show();
                        } else if ((key == 4)) {
                            $("#fiveActivity a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                            $("#fiveActivity a img").attr("src", 'resource/image/' + article.resource[0].pathName);
                            $("#fiveActivity a div p").html(article.titleName);
                            $("#fiveActivity a div p:eq(1)").append(article.content);
                            $("#fiveActivity").show();
                        }
                    });

                    //上一页
                    $("#reviousId").click(function () {
                        if (pageNo != 1) {
                            getActivity(oneClass, twoClass, pageNo - 1, '');
                        }
                    });

                    //下一页
                    $("#nextId").click(function () {
                        if (pageNo != pages) {
                            getActivity(oneClass, twoClass, pageNo + 1, '');
                        }
                    });

                    //点击页码
                    $("#clickPage").click(function () {
                        var thisClickPage = $(this).html();
                        getActivity(oneClass, twoClass, thisClickPage, '');
                    });


                }

            });

        }

        //获取数据公用类
        function getInfo(oneClass, twoClass, pageNo, pageSize) {
            //设置默认值
            if (pageNo == undefined || pageNo == '') {
                pageNo = 1;
            }
            if (pageSize == undefined || pageSize == '') {
                pageSize = 5;
            }

            $(function () {
                $.ajax({
                        type: "POST",
                        async: false,
                        url: getRootPath() + "/CI.do",
                        data: "oneClass=" + oneClass + "&twoClass=" + twoClass + "&pageNo=" + pageNo + "&pageSize=" + pageSize,
                        success: function (data) {

                            //顶部大图
                            $("#tvTopBigImage").attr("src", 'resource/image/' + data.data.topImagePath);

                            //当前页
                            pageNo = data.data.articleContext.pageNo;

                            //总页数
                            pages = data.data.articleContext.pages;

                            //清空处理
                            $("#pageUl").empty()

                            //上一页按钮样式
                            $("#pageUl").append('<li><a id="reviousId" aria-label="Previous"><span aria-hidden="true"><img src="img/leftb.png" alt=""></span></a></li>');

                            /*页码遍历*/
                            if (pageNo + 4 < pages) {
                                for (var thisP = pageNo; thisP <= pageNo + 4; thisP++) {
                                    if (thisP == pageNo) {
                                        //设定当前背景颜色
                                        $("#pageUl").append('<li><a class="flipcol" >' + thisP + '</a></li>');
                                    } else {
                                        $("#pageUl").append('<li><a id="clickPage">' + thisP + '</a></li>');
                                    }
                                }
                            }
                            else {
                                for (var thisP = pageNo; thisP <= pages; thisP++) {
                                    if (thisP == pageNo) {
                                        //设定当前背景颜色
                                        $("#pageUl").append('<li><a class="flipcol" >' + thisP + '</a></li>');
                                    } else {
                                        $("#pageUl").append('<li><a id="clickPage">' + thisP + '</a></li>');
                                    }
                                }
                            }

                            //下一页按钮样式
                            $("#pageUl").append('<li><a id="nextId" aria-label="Next"><span aria-hidden="true"><img class="imgr" src="img/rightb.png" alt=""></span></a></li>');

                            //默认隐藏
                            $("#oneId").hide();
                            $("#twoId").hide();
                            $("#threeId").hide();
                            $("#fourId").hide();
                            $("#fiveId").hide();
                            $("#sixId").hide();

                            //遍历数据
                            $.each(data.data.articleContext.dataList, function (key, article) {
                                if (key == 0) {
                                    $("#oneId a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                                    $("#oneId a div img").attr("src", 'resource/image/' + article.resource[0].pathName);
                                    $("#oneId a div:eq(1) dl dt").text(article.titleName);
                                    $("#oneId a div:eq(1) dl dd").append(article.content);
                                    $("#oneId").show();
                                } else if (key == 1) {
                                    $("#twoId a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                                    $("#twoId a div img").attr("src", 'resource/image/' + article.resource[0].pathName);
                                    $("#twoId a div dl dt").text(article.titleName);
                                    $("#twoId a div dl dd").append(article.content);
                                    $("#twoId").show();
                                } else if (key == 2) {
                                    $("#threeId a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                                    $("#threeId a div img").attr("src", 'resource/image/' + article.resource[0].pathName);
                                    $("#threeId a div:eq(1) dl dt").text(article.titleName);
                                    $("#threeId a div:eq(1) dl dd").append(article.content);
                                    $("#threeId").show();
                                } else if (key == 3) {
                                    $("#fourId a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                                    $("#fourId a div img").attr("src", 'resource/image/' + article.resource[0].pathName);
                                    $("#fourId a div dl dt").text(article.titleName);
                                    $("#fourId a div dl dd").append(article.content);
                                    $("#fourId").show();
                                } else if (key == 4) {
                                    $("#fiveId a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                                    $("#fiveId a div img").attr("src", 'resource/image/' + article.resource[0].pathName);
                                    $("#fiveId a div:eq(1) dl dt").text(article.titleName);
                                    $("#fiveId a div:eq(1) dl dd").append(article.content);
                                    $("#fiveId").show();
                                } else if (key == 5) {
                                    $("#sixId a").attr("href", 'adv.html?conId=' + article.resource[0].artId);
                                    $("#sixId a div img").attr("src", 'resource/image/' + article.resource[0].pathName);
                                    $("#sixId a div dl dt").text(article.titleName);
                                    $("#sixId a div dl dd").append(article.content);
                                    $("#sixId").show();
                                }


                            });
                            //上一页
                            $("#reviousId").click(function () {
                                if (pageNo != 1) {
                                    getInfo(oneClass, twoClass, pageNo - 1, '');
                                }
                            });
                            //下一页
                            $("#nextId").click(function () {
                                if (pageNo != pages) {
                                    if (oneClass == 'SmarterLife') {
                                        getInfo(oneClass, twoClass, pageNo + 1, 6);
                                    } else {
                                        getInfo(oneClass, twoClass, pageNo + 1, '');
                                    }
                                }
                            });

                            //点击页码
                            $("#clickPage").click(function () {
                                var thisClickPage = $(this).html();
                                if(oneClass == 'SmarterLife'){
                                    getInfo(oneClass, twoClass, thisClickPage, 6);
                                }else{
                                    getInfo(oneClass, twoClass, thisClickPage, '');
                                }
                            });

                        }
                    }
                )
                ;
            })

        }

//判断是否是电视+页面
        if (curWwwPath.indexOf("/inn-pro.html") > 0) {
            getInfo('TvPlus', 'Context', '', '');
        }//智慧生活
        else if (curWwwPath.indexOf("/tv-pro.html") > 0) {
            getInfo('SmarterLife', 'Context', '', 6);
        }//技术运营
        else if (curWwwPath.indexOf("/jishu.html") > 0) {
            getInfo('Operating', 'Context', '', '');
        }
//合作伙伴
        else if (curWwwPath.indexOf("/banquan.html") > 0) {
            $.ajax({
                type: "POST",
                async: false,
                url: getRootPath() + "/CI.do",
                data: "oneClass=Copyright&twoClass=Context&pageNo=1&pageSize=20",
                success: function (data) {
                    //顶部大图
                    $("#tvTopBigImage").attr("src", 'resource/image/' + data.data.topImagePath);
                    $.each(data.data.articleContext.dataList[0].resource, function (key, resources) {
                        $("#clearDiv").append('<div class="banquan p col-lg-4"><dl class=""><dt><img src=resource/image/' + resources.pathName + '></dt><dd>' + resources.name + '</dd></dl></div>');
                    });
                    $("#clearDiv").append('<div class="clear"></div>');
                }
            });

        }
//最新活动
        else if (curWwwPath.indexOf("/Advertising.html") > 0) {
            getActivity('LatestActivity', 'Home', '', '')
        }


    })
;